﻿using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Sharp.Portal.Mvc.Bootstrap
{
    public sealed class SelectBoxBuilder : TagElementBuilder<SelectBoxBuilder>
    {
        public SelectBoxBuilder()
        {
            TagBuilder.AddCssClass("input-group");
            TextInput = new InputBuilder();
            ValueInput = new InputBuilder();
        }

        public override string TagName => "div";
        public string ValueField { get; set; }

        public string DisplayField { get; set; }
        /// <summary>
        /// WindowUrl
        /// </summary>
        public string WindowUrl { get; set; }

        public string Text { get; set; }
        public string Value { get; set; }

        internal InputBuilder TextInput { get; set; }
        internal InputBuilder ValueInput { get; set; }

        public override string ToHtmlString()
        {
            var name = TagBuilder.Attributes["name"];
            var id = TagBuilder.Attributes["id"];
            var windowUrlParm = $"?selectTextControl={name}";
            var builder = new StringBuilder();
            TagBuilder.Attributes["id"] = "div_" + name;
            builder.AppendLine(TagBuilder.ToString(TagRenderMode.StartTag));
            builder.AppendLine(TextInput.Id(name).Name(name).Attribute("readonly", "readonly").Value(Text).ToHtmlString());
            builder.AppendFormat("<span class=\"input-group-addon\"><i class=\"fa fa-check\" id=\"{1}Trigger\" data-toggle=\"ajaxModal\" data-valueField=\"{4}\" href=\"{2}\"></i></span>", "", name, WindowUrl, "", ValueField);

            builder.AppendLine(ValueInput.Type().Hidden().Id(ValueField).Value(Value).ToHtmlString());

            builder.AppendLine(TagBuilder.ToString(TagRenderMode.EndTag));

            return new HtmlString(builder.ToString()).ToHtmlString();
        }

    }
}
